/**
 * 
 */
package ads.engenharia.dal;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.entity.Professor;
import ads.engenharia.entity.Schedule;

/**
 * @author Lanedraex
 *
 */
public class ProfessorScheduleDAO {

	private String sql;
	private String tableName = "\"Professor_Schedule\"";

	private EntityManager em = new EntityManager() {

		@Override
		public Object treatResultSet(ResultSet resultSet) throws SQLException {
			Professor professor = new Professor();

			professor.schedule(searchProfessorSchedule(professor.id()));
			return professor;
		}
	};

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void save(Professor professor) throws SQLException {

		sql = " insert into " + tableName
				+ " ( professor_id, day, time, hour ) ";

		for (Schedule.Day day : professor.schedule().Days) {

			for (Schedule.Time time : professor.schedule().Times) {

				for (Schedule.Hour hour : professor.schedule().Hours) {

					em.executeQuery(sql, professor.id(), day.value(),
							time.value(), hour.value());
				}
			}
		}
	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void update(Professor professor) throws SQLException {

		sql = " update " + tableName
				+ " set day = ?, time = ?, hour = ? where professor_id = ?";

		for (Schedule.Day day : professor.schedule().Days) {

			for (Schedule.Time time : professor.schedule().Times) {

				for (Schedule.Hour hour : professor.schedule().Hours) {

					em.executeQuery(sql, day.value(), time.value(),
							hour.value(), professor.id());
				}
			}
		}
	}

	/**
	 * 
	 * @param professor
	 * @return completedSchedule
	 * @throws SQLException
	 */
	public Schedule searchProfessorSchedule(Professor professor)
			throws SQLException {

		sql = " select distinct day, time, hour from " + tableName
				+ " where professor_id = ? order by day, time, hour ";

		@SuppressWarnings("unchecked")
		ArrayList<Schedule> schedules = (ArrayList<Schedule>) em.getResultList(
				sql, professor.id());

		Schedule completedSchedule = new Schedule();

		// Adiciona cada parte do array de Schedule num schedule final
		for (Schedule schedule : schedules) {
			completedSchedule.Days.add(schedule.Days.get(0));
			completedSchedule.Times.add(schedule.Times.get(0));
			completedSchedule.Hours.add(schedule.Hours.get(0));
		}

		return completedSchedule;
	}

	/**
	 * 
	 * @param id
	 * @return completedSchedule
	 * @throws SQLException
	 */
	public Schedule searchProfessorSchedule(int id) throws SQLException {

		sql = " select distinct day, time, hour from " + tableName
				+ " where professor_id = ? order by day, time, hour ";

		@SuppressWarnings("unchecked")
		ArrayList<Schedule> schedules = (ArrayList<Schedule>) em.getResultList(
				sql, id);

		Schedule completedSchedule = new Schedule();

		// Adiciona cada parte do array de Schedule num schedule final
		for (Schedule schedule : schedules) {
			completedSchedule.Days.add(schedule.Days.get(0));
			completedSchedule.Times.add(schedule.Times.get(0));
			completedSchedule.Hours.add(schedule.Hours.get(0));
		}

		return completedSchedule;
	}
}
